package com.zmzncs.lmtc.module.park.controller;


import com.zmzncs.lmtc.common.config.ClientConfig;
import com.zmzncs.lmtc.common.pojo.Rest;
import com.zmzncs.lmtc.common.pojo.tengda.TengdaForm;
import com.zmzncs.lmtc.common.util.RestUtil;
import com.zmzncs.lmtc.common.util.TengdaUtil;
import com.zmzncs.lmtc.module.park.service.ParkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import static com.zmzncs.lmtc.module.park.service.impl.ParkServiceImpl.tengdaParkFormList;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 任建波
 * @since 2020-10-09
 */
@Api(tags = "停车场信息")
@RestController
@RequestMapping("/park/info")
@Slf4j
public class ParkController {

    @Autowired
    private TengdaUtil tengdaUtil;
    @Autowired
    private ParkService parkService;
    @Autowired
    private ClientConfig clientConfig;

    @ApiOperation(value = "添加订阅")
    @GetMapping(value = "/subscribe")
    public Rest subscribe(String bolianParkId) {
        try {
            clientConfig.getClient().subscribe(bolianParkId, 1);
        } catch (MqttException e) {
            e.printStackTrace();
        }

        return RestUtil.success("订阅成功");
    }

    @ApiOperation(value = "车辆类型")
    @GetMapping(value = "/car-types")
    public TengdaForm carTypes(String bolianParkId) throws InterruptedException {
        //  获取车辆类型
        String uuid = UUID.randomUUID().toString();
        tengdaUtil.getCarTypes(bolianParkId, uuid);

        //  缓存中查询响应数据
        for (int i=0; i<10; i++) {
            Thread.sleep(500);

            //  如果uuid相同，则响应数据
            Optional<TengdaForm> first = tengdaParkFormList.stream().filter(tengdaForm -> tengdaForm.getUuid().equals(uuid)).findFirst();
            if(first.isPresent()){
                //  删除缓存
                parkService.deleteCache(first.get());
                return first.get();
            }
        }

        TengdaForm tengdaForm = new TengdaForm();
        tengdaForm.setCode(0);
        tengdaForm.setParkCode(bolianParkId);
        tengdaForm.setServiceId("manage.cartypes");
        tengdaForm.setUuid(uuid);

        return tengdaForm;
    }

    @ApiOperation(value = "获取通道列表")
    @GetMapping(value = "/channels")
    public TengdaForm channels(@RequestParam String bolianParkId) throws InterruptedException {
        String uuid = UUID.randomUUID().toString();
        tengdaUtil.channels(bolianParkId, uuid);

        //  缓存中查询响应数据
        for (int i=0; i<10; i++) {
            Thread.sleep(500);

            //  如果uuid相同，则响应数据
            Optional<TengdaForm> first = tengdaParkFormList.stream().filter(tengdaForm -> tengdaForm.getUuid().equals(uuid)).findFirst();

            if(first.isPresent()){
                //  删除缓存
                parkService.deleteCache(first.get());
                return first.get();
            }
        }

        TengdaForm tengdaForm = new TengdaForm();
        tengdaForm.setCode(0);
        tengdaForm.setParkCode(bolianParkId);
        tengdaForm.setServiceId("manage.channels");
        tengdaForm.setUuid(uuid);

        return tengdaForm;
    }

    @ApiOperation(value = "开闸")
    @PostMapping(value = "/gate-on-off")
    public TengdaForm gateOnOff(String bolianParkId, String channelId, String action, String opt) throws InterruptedException {
        String uuid = UUID.randomUUID().toString();
        tengdaUtil.gateOnOff(bolianParkId, channelId, action, opt, uuid);

        //  缓存中查询响应数据
        for (int i=0; i<10; i++) {
            Thread.sleep(500);

            //  如果uuid相同，则响应数据
            Optional<TengdaForm> first = tengdaParkFormList.stream().filter(tengdaForm -> tengdaForm.getUuid().equals(uuid)).findFirst();
            if(first.isPresent()){
                //  删除缓存
                parkService.deleteCache(first.get());
                return first.get();
            }
        }

        TengdaForm tengdaForm = new TengdaForm();
        tengdaForm.setCode(0);
        tengdaForm.setParkCode(bolianParkId);
        tengdaForm.setServiceId("manage.channels");
        tengdaForm.setUuid(uuid);

        return tengdaForm;
    }

}

